package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UsersDao;
import dao.impl.UsersDaoImpl;
import entity.Users;
import utils.GetFieldUtils;
import utils.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserMethodServlet", urlPatterns = "/bigProjects/UserMethodServlet")

public class UserMethodServlet extends HttpServlet {
    //注入无效Dao,只能在方法中写了


    //登录请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //说是使用多态,提高耦合度
        UsersDao usersDao = new UsersDaoImpl();
        // 设置响应的内容类型为 JSON
        resp.setContentType("application/json");
        //设置ContentType字段值
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String phoneNumber = req.getParameter("phoneNumber");
        String password = req.getParameter("password");
        Users users = usersDao.selectOneUser(phoneNumber, password);
        if (users != null) {
            ObjectMapper mapper = new ObjectMapper();
            resp.getWriter().write(ResponseResult.success("登录成功", users).toString());
        } else resp.getWriter().write(ResponseResult.failure("登录失败").toString());
    }

    //注册请求
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UsersDao usersDao = new UsersDaoImpl();
        /**/
        // 设置响应的内容类型为 JSON
        resp.setContentType("application/json");
        //设置ContentType字段值
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String userString = GetFieldUtils.getRequestPayload(req);
        ObjectMapper mapper = new ObjectMapper();
        //读取前端传递的Json字符串,转成Users对象
        Users users = mapper.readValue(userString, Users.class);
        boolean b = usersDao.insertUser(users);
        if (b) resp.getWriter().write(ResponseResult.success().toString());
        else resp.getWriter().write(ResponseResult.failure("注册失败").toString());
    }

    //修改信息请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UsersDao usersDao = new UsersDaoImpl();

        // 设置响应的内容类型为 JSON
        resp.setContentType("application/json");
        //设置ContentType字段值
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String userString = GetFieldUtils.getRequestPayload(req);
        ObjectMapper mapper = new ObjectMapper();
        Users users = mapper.readValue(userString, Users.class);
        boolean b = usersDao.updateById(users);
        if (b) resp.getWriter().write(ResponseResult.success().toString());
        else resp.getWriter().write(ResponseResult.failure("修改失败").toString());
    }

    //删除用户;id参数
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UsersDao usersDao = new UsersDaoImpl();

        // 设置响应的内容类型为 JSON
        resp.setContentType("application/json");
        //设置ContentType字段值
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Integer id = Integer.valueOf(req.getParameter("id"));
        int i = usersDao.deleteById(id);
        if (i > 0) resp.getWriter().write(ResponseResult.success().toString());
        else resp.getWriter().write(ResponseResult.failure("删除失败").toString());
    }
}
